package com.woniu.clientServer.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.clientServer.dto.CustomerApplicationDto;
import com.woniu.clientServer.pojo.Customer;
import com.woniu.clientServer.service.BankService;
import com.woniu.clientServer.service.CustomerApplicationService;
import com.woniu.clientServer.service.CustomerService;
import com.woniu.clientServer.service.ProvinceService;
import com.woniu.common.utils.PageData;
import com.woniu.common.utils.ResponseData;
import com.woniu.server.dto.ProjectInfoDto;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


@RestController
@RequestMapping("/client")
public class ClientController {
    @Autowired
    private CustomerService customerService;
    @Autowired
    private BankService bankService;
    @Autowired
    private ProvinceService provinceService;
    @Autowired
    private CustomerApplicationService customerApplicationService;

    /**
     * 查询所有
     * @return
     */
    @PostMapping("/queryAll")
    public ResponseData<?> queryAll(@RequestBody PageData<Customer> pageData) {
        PageHelper.startPage(pageData.getPageNum(), pageData.getPageSize());
        List<Customer> list = customerService.lambdaQuery().list();
        PageInfo<Customer> pageInfo = new PageInfo<>(list);
        return new ResponseData<>().ok(pageInfo);
    }
    @GetMapping("/queryAll2")
    public ResponseData<?> queryAll2() {
        return new ResponseData<>().ok(customerService.lambdaQuery().list());
    }
    /**
     * 根据code查询
     * @param code
     * @return
     */
    @GetMapping("/queryByCode/{code}")
    public ResponseData<?> queryByCode(@PathVariable("code") String code) {
        return new ResponseData<>().ok( customerService.lambdaQuery().eq(Customer::getCode, code).one());
    }
    /**
     * 查询银行
     */
    @PostMapping("/queryBank")
    public ResponseData<?> queryBank() {
        return new ResponseData<>().ok(bankService.lambdaQuery().list());
    }
    /**
     * 查询地区
     */
    @PostMapping("/queryRegion")
    public ResponseData<?> queryRegion() {
        return new ResponseData<>().ok(provinceService.lambdaQuery().list());
    }
    /**
     * 新增客户
     */
    @PostMapping("/clientApp")
    public ResponseData<?> clientApp(@RequestBody CustomerApplicationDto customerApplicationDto) {
        return customerApplicationService.clientApp(customerApplicationDto);
    }
    /**
     * 删除客户
     */
    @GetMapping("/deleteByCode/{code}")
    public ResponseData<?> deleteByCode(@PathVariable("code") String code) {
        return new ResponseData<>().ok( customerService.lambdaUpdate().eq(Customer::getCode, code).remove());
    }
}
